package com.guigu.controller.manufacture;


import com.guigu.pojo.manufacture.*;
import com.guigu.pojo.stock.SGather;
import com.guigu.pojo.stock.SGatherDetails;
import com.guigu.service.manufacture.*;
import com.guigu.service.stock.SGatherDetailsService;
import com.guigu.service.stock.SGatherService;
import com.guigu.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/m-proceduring")
public class MProceduringController {

    @Autowired
    private MProcedureService mProcedureService;

    @Autowired
    private MManufactureService mManufactureService;

    @Autowired
    private MProcedureService procedureService;

    @Autowired
    private MProcedureModuleService mProcedureModuleService;

    @Autowired
    private MProceduringService mProceduringService;
    
    @Autowired
    private MProcedureModulingService mProcedureModulingService;

    @Autowired
    private SGatherService sGatherService;

    @Autowired
    private SGatherDetailsService sGatherDetailsService;

    @GetMapping("/Inquiry_production_registration")
    public String Inquiry_production_registration(){
        return "jsp/manufacture/f/Inquiry_production_registration";
    }

    @GetMapping("/Inquiry_producttion_registration_query")
    public String Inquiry_producttion_registration_query(){
        return "jsp/manufacture/f/Inquiry_producttion_registration_query";
    }

    @GetMapping("/production_registration")
    public String production_registration(){
        return "jsp/manufacture/f/production_registration";
    }

    @GetMapping("/production_registration_review")
    public String production_registration_review(){
        return "jsp/manufacture/f/production_registration_review";
    }

    @GetMapping("/production_registration_review_sub")
    public String production_registration_review_sub(){
        return "jsp/manufacture/f/production_registration_review_sub";
    }

    @GetMapping("/production_registration_sub")
    public String production_registration_sub(){
        return "jsp/manufacture/f/production_registration_sub";
    }

    @PostMapping("/registered")
    @ResponseBody
    public Boolean registered(@RequestBody HashMap<String,Object> map){
        System.out.println(map);
        Integer id = null;
        String register = "";
        String manufactureId = "";
        String tag1 = "";
        String tag2 = "";
        String HourAmount = "";
        if(map.containsKey("id")){
            id = Integer.parseInt(map.get("id").toString());
        }
        if(map.containsKey("register2")){
            register = map.get("register2").toString();
        }
        if(map.containsKey("manufactureId")){
            manufactureId = map.get("manufactureId").toString();
        }
        if(map.containsKey("tag1")){
            tag1 = map.get("tag1").toString();
        }
        if(map.containsKey("tag2")){
            tag2 = map.get("tag2").toString();
        }
        if(map.containsKey("HourAmount")){
            HourAmount = map.get("HourAmount").toString();
        }
        // 1.根据manufactureId查询manufacture
        MManufacture manufacture = mManufactureService.getManufactureByManufactureId(manufactureId);
        manufacture.setManufactureProcedureTag("1");
        // 2.根据id查询procedure
        MProcedure mProcedure = procedureService.getById(id);
        mProcedure.setProcedureFinishTag(tag1);
        mProcedure.setProcedureTransferTag(tag2);

        List<MProcedureModule> mProcedureModules = mProcedureModuleService.getProcedureModuleByParentId(mProcedure.getId());

        MProceduring mProceduring = new MProceduring();
        mProceduring.setParentId(manufacture.getId());
        mProceduring.setDetailsNumber(mProcedure.getDetailsNumber());
        mProceduring.setProcedureId(mProcedure.getProcedureId());
        mProceduring.setProcedureName(mProcedure.getProcedureName());
        mProceduring.setLabourHourAmount(new BigDecimal(HourAmount));
        mProceduring.setCostPrice(mProcedure.getCostPrice());
        mProceduring.setSubtotal(mProceduring.getLabourHourAmount().multiply(mProceduring.getCostPrice()));
        mProceduring.setProcedureDescribe("");
        mProceduring.setRegCount(BigDecimal.valueOf(1));
        mProceduring.setProcedureResponsiblePerson(register);
        mProceduring.setRegister(register);
        mProceduring.setRegisterTime(new Date());
        mProceduring.setChecker("0");

        // 3.保存mProceduring
        boolean save = mProceduringService.save(mProceduring);

        HashMap<String ,Object> map1 = (HashMap<String, Object>) map.get("realAmountArr");
        for (int i = 0; i < mProcedureModules.size(); i++) {
            MProcedureModule mProcedureModule = mProcedureModules.get(i);
            String productId = mProcedureModule.getProductId();
            String num = map1.get(productId).toString();

            MProcedureModuling mProcedureModuling = new MProcedureModuling();
            mProcedureModuling.setParentId(mProceduring.getId());
            mProcedureModuling.setDetailsNumber(mProcedureModule.getDetailsNumber());
            mProcedureModuling.setProductId(mProcedureModule.getProductId());
            mProcedureModuling.setProductName(mProcedureModule.getProductName());
            mProcedureModuling.setCostPrice(mProcedureModule.getCostPrice());
            mProcedureModuling.setAmount(BigDecimal.valueOf(Long.valueOf(num)));
            mProcedureModuling.setSubtotal(mProcedureModuling.getCostPrice().multiply(mProcedureModuling.getAmount()));

            boolean save1 = mProcedureModulingService.save(mProcedureModuling);
            if(!save1){
                return false;
            }

        }
        boolean b = mManufactureService.updateById(manufacture);
        if(!b){
            return false;
        }
        boolean b1 = procedureService.updateById(mProcedure);

        return b1;
    }

    @GetMapping("/selectArrByParentId")
    @ResponseBody
    public ArrayList<MProceduring> selectArrByParentId(HttpServletRequest req){
        Integer id = Integer.parseInt(req.getParameter("id"));
        ArrayList<MProceduring> mProcedurings = mProceduringService.selectArrByParentId(id);
        return mProcedurings;
    }

    @PostMapping("/registrationAudit")
    @ResponseBody
    public Boolean registrationAudit(@RequestBody HashMap<String,Object> map) {
        Integer id = null;
        String realLabourHourAmount = "";//实际工时
        String realSubtotal = "";//实际成本
        String realModuleSubtotal = "";//实际物料成本
        String procedureFinishTag = "";
        String procedureTransferTag = "";
        String checker = "";
        String remark = "";
        if(map.containsKey("id")){
            id = Integer.parseInt(map.get("id").toString());
        }
        if(map.containsKey("realLabourHourAmount")){
            realLabourHourAmount = map.get("realLabourHourAmount").toString();
        }
        if(map.containsKey("realSubtotal")){
            realSubtotal = map.get("realSubtotal").toString();
        }
        if(map.containsKey("realModuleSubtotal")){
            realModuleSubtotal = map.get("realModuleSubtotal").toString();
        }
        if(map.containsKey("procedureFinishTag")){
            procedureFinishTag = map.get("procedureFinishTag").toString();
        }
        if(map.containsKey("procedureTransferTag")){
            procedureTransferTag = map.get("procedureTransferTag").toString();
        }
        if(map.containsKey("checker")){
            checker = map.get("checker").toString();
        }
        if(map.containsKey("remark")){
            remark = map.get("remark").toString();
        }
        MProcedure mProcedure = mProcedureService.getById(id);

        MManufacture manufacture = mManufactureService.getById(mProcedure.getParentId());

        ArrayList<MProceduring> mProcedurings = mProceduringService.selectArrByParentId(mProcedure.getParentId());
        if(mProcedurings.size() == 0){

        }else {}
        MProceduring mProceduring = mProcedurings.get(0);
        List<MProcedureModule> mProcedureModules = mProcedureModuleService.getProcedureModuleByParentId(mProcedure.getId());

        ArrayList<MProcedureModuling> mProcedureModulings = mProcedureModulingService.getProcedureModulingByParentId(mProceduring.getId());

        manufacture.setRealModuleCostPriceSum(BigDecimal.valueOf(Long.valueOf(realModuleSubtotal)));
        manufacture.setRealLabourCostPriceSum(BigDecimal.valueOf(Long.valueOf(realLabourHourAmount)));

        manufacture.setManufactureProcedureTag("0");

        mProcedure.setRealLabourHourAmount(BigDecimal.valueOf(Long.valueOf(realLabourHourAmount)));
        mProcedure.setRealSubtotal(BigDecimal.valueOf(Long.valueOf(realSubtotal)));
        mProcedure.setRealModuleSubtotal(BigDecimal.valueOf(Long.valueOf(realModuleSubtotal)));
        mProcedure.setProcedureFinishTag(procedureFinishTag);
        mProcedure.setProcedureTransferTag(procedureTransferTag);


        for (int i = 0; i < mProcedureModules.size(); i++) {
            MProcedureModule mProcedureModule = mProcedureModules.get(i);
            for (int j = 0; j < mProcedureModulings.size(); j++) {
                MProcedureModuling mProcedureModuling = mProcedureModulings.get(j);
                if(mProcedureModuling.getProductId().equals(mProcedureModule.getProductId())){
                    BigDecimal add = mProcedureModule.getRealAmount()!=null?mProcedureModule.getRealAmount().add(mProcedureModuling.getAmount()):mProcedureModuling.getAmount();
                    mProcedureModule.setRealAmount(add);
                    BigDecimal multiply = mProcedureModule.getRealSubtotal()!=null?mProcedureModule.getRealSubtotal().add(mProcedureModuling.getSubtotal()):mProcedureModuling.getSubtotal();
                    mProcedureModule.setRealSubtotal(multiply);
                    boolean b = mProcedureModuleService.updateById(mProcedureModule);
                    if(!b){
                        return false;
                    }
                }
            }
        }
        mProceduring.setChecker(checker);
        mProceduring.setCheckTime(new Date());
        if (mProceduring.getRegCount() != null) {
            mProceduring.setRegCount(mProceduring.getRegCount().add(BigDecimal.valueOf(1)));
        } else {
            mProceduring.setRegCount(BigDecimal.valueOf(1));
        }
        boolean b = mProceduringService.updateById(mProceduring);
        if(!b){
            return false;
        }
        boolean b1 = mManufactureService.updateById(manufacture);
        if(!b1){
            return false;
        }
        boolean b2 = mProcedureService.updateById(mProcedure);
        if(!b2){
            return false;
        }

        return true;
    }

    @GetMapping("/handover")
    @ResponseBody
    public Boolean handover(HttpServletRequest req){
        Integer id = Integer.parseInt(req.getParameter("id"));
        MProcedure mProcedure = mProcedureService.getById(id);
        mProcedure.setProcedureFinishTag("2");
        mProcedure.setProcedureTransferTag("1");
        boolean b = mProcedureService.updateById(mProcedure);
        if(!b){
            return false;
        }
        MManufacture manufacture = mManufactureService.getById(mProcedure.getParentId());
        manufacture.setManufactureProcedureTag("1");
        boolean b1 = mManufactureService.updateById(manufacture);
        return b1;
    }

    @PostMapping("/handoverAudits")
    @ResponseBody
    public Boolean handoverAudits(@RequestBody HashMap<String,Object> map){
        Integer id = null;
        String procedureFinishTag = "";
        String procedureTransferTag = "";
        String checker = "";
        String remark = "";
        if(map.containsKey("id")){
            id = Integer.parseInt(map.get("id").toString());
        }
        if(map.containsKey("procedureFinishTag")){
            procedureFinishTag = map.get("procedureFinishTag").toString();
        }
        if(map.containsKey("procedureTransferTag")){
            procedureTransferTag = map.get("procedureTransferTag").toString();
        }
        if(map.containsKey("checker")){
            checker = map.get("checker").toString();
        }
        if(map.containsKey("remark")){
            remark = map.get("remark").toString();
        }
        MProcedure mProcedure = mProcedureService.getById(id);
        mProcedure.setProcedureFinishTag(procedureFinishTag);
        mProcedure.setProcedureTransferTag(procedureTransferTag);
        boolean b = mProcedureService.updateById(mProcedure);
        if(!b){
            return false;
        }
        MManufacture manufacture = mManufactureService.getById(mProcedure.getParentId());
        BigDecimal realModuleCostPriceSum = manufacture.getRealModuleCostPriceSum();
        manufacture.setRealModuleCostPriceSum(realModuleCostPriceSum.add(mProcedure.getModuleSubtotal()));
        BigDecimal realLabourCostPriceSum = manufacture.getRealLabourCostPriceSum();
        manufacture.setRealLabourCostPriceSum(realLabourCostPriceSum.add(mProcedure.getRealSubtotal()));
        manufacture.setManufactureProcedureTag("0");
        boolean b1 = false;
        ArrayList<MProcedure> mProcedures = mProcedureService.selectArrByParentId(manufacture.getId());
        MProcedure mProcedure1 = mProcedures.get(mProcedures.size() - 1);
        if(mProcedure1.getParentId()==manufacture.getId()){
            if(mProcedure1.getProcedureFinishTag().equals("1")&&mProcedure1.getProcedureTransferTag().equals("1")){
                manufacture.setManufactureProcedureTag("2");
                BigDecimal divide = manufacture.getModuleCostPriceSum().divide(manufacture.getAmount());
                manufacture.setRealModuleCostPriceSum(manufacture.getModuleCostPriceSum());
                BigDecimal divide1 = manufacture.getRealModuleCostPriceSum().divide(divide);
                manufacture.setTestedAmount(divide1);

                SGather endSGather = sGatherService.getEndSGather();
                CodeUtils codeUtils = new CodeUtils();
                String gatherboundCode = codeUtils.getGatherboundCode(endSGather);

                SGather sGather = new SGather();
                sGather.setGatherId(gatherboundCode);
                sGather.setStorer(manufacture.getDesigner());
                sGather.setReason("内部生产");
                sGather.setAmountSum(manufacture.getTestedAmount());
                sGather.setCostPriceSum(manufacture.getRealModuleCostPriceSum());
                sGather.setGatheredAmountSum(manufacture.getTestedAmount());

                sGather.setCheckTag("1");
                sGather.setGatherTag("1");
                sGather.setGatherCheckTag("0");

                boolean save = sGatherService.save(sGather);
                if (!save){
                    return false;
                }
                SGatherDetails sGatherDetails = new SGatherDetails();
                sGatherDetails.setParentId(sGather.getId());
                sGatherDetails.setProductId(manufacture.getProductId());
                sGatherDetails.setProductName(manufacture.getProductName());
                sGatherDetails.setProductDescribe(manufacture.getProductDescribe());
                sGatherDetails.setAmount(manufacture.getTestedAmount());
                sGatherDetails.setAmountUnit("件");
                sGatherDetails.setCostPrice(divide);
                sGatherDetails.setSubtotal(manufacture.getRealModuleCostPriceSum());
                sGatherDetails.setGatheredAmount(manufacture.getTestedAmount());
                sGatherDetails.setGatherTag("0");

                boolean save1 = sGatherDetailsService.save(sGatherDetails);

                if (!save1){
                    return false;
                }



            }
        }
        b1 = mManufactureService.updateById(manufacture);
        return b1;
    }

}

